﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace SHomeWorkshop.LunarPresentation.Adorners
{
    /// <summary>
    /// 文本块的折叠标记。
    /// </summary>
    public class CollapsedAdorner : Adorner
    {
        /// <summary>
        /// 文本块的折叠标记。
        /// </summary>
        /// <param name="adornedElement">应传入文本块。</param>
        public CollapsedAdorner(Expressions.BaseText adornedElement)
            : base(adornedElement)
        {
            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

            this.masterBt = adornedElement;

            this.Cursor = Cursors.Hand;
            this.ToolTip = CollapsedAdorner.CustomToolTip;

            this.MouseLeftButtonDown += new MouseButtonEventHandler(CollapsedAdorner_MouseLeftButtonDown);
            this.MouseLeftButtonUp += new MouseButtonEventHandler(CollapsedAdorner_MouseLeftButtonUp);
            this.MouseRightButtonDown += new MouseButtonEventHandler(CollapsedAdorner_MouseRightButtonDown);

            this.MouseEnter += new MouseEventHandler(CollapsedAdorner_MouseEnter);
            this.MouseLeave += new MouseEventHandler(CollapsedAdorner_MouseLeave);
        }

        public static readonly string CustomToolTip = "折叠/展开";

        void CollapsedAdorner_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (masterBt != null && masterBt.Editor != null)
            {
                if (masterBt.Editor.SelectedExpression != masterBt)
                {
                    masterBt.Editor.SelectedExpression = masterBt;
                }
            }

            e.Handled = true;
        }

        void CollapsedAdorner_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //完成拖动。
            Globals.DropExpression(this.masterBt);
        }

        void CollapsedAdorner_MouseLeave(object sender, MouseEventArgs e)
        {
            this.InvalidateVisual();
        }

        void CollapsedAdorner_MouseEnter(object sender, MouseEventArgs e)
        {
            this.InvalidateVisual();
        }

        void CollapsedAdorner_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (masterBt == null) return;

            List<Expressions.Expression> subIndentExpressions = masterBt.GetSubIndentExpressions();

            if (subIndentExpressions == null || subIndentExpressions.Count <= 0)//注意，前一个用属性，后一个用字段。
            {
                MessageBox.Show("此文本块没有“下级”公式，“折叠”操作没有意义。",
                       Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (masterBt.IsCollapsed)
            {
                ModifingItem mi = new ModifingItem();
                Action actUnCollapsed = new Action(masterBt.ExID, -1, masterBt.GetType().Name,
                    "IsCollapsed", true.ToString(), false.ToString());

                masterBt.IsCollapsed = false;

                mi.Add(actUnCollapsed);

                masterBt.Editor.Do(mi);
            }
            else
            {
                ModifingItem mi = new ModifingItem();
                Action actUnCollapsed = new Action(masterBt.ExID, -1, masterBt.GetType().Name,
                    "IsCollapsed", false.ToString(), true.ToString());

                masterBt.IsCollapsed = true;

                mi.Add(actUnCollapsed);

                masterBt.Editor.Do(mi);
            }

            masterBt.Editor.SelectedExpression = masterBt;
            e.Handled = true;
        }

        private Expressions.BaseText masterBt;

        /// <summary>
        /// 使用本折叠标记的文本块。
        /// </summary>
        public Expressions.BaseText MasterBt
        {
            get { return masterBt; }
            set { masterBt = value; }
        }

        static CollapsedAdorner()
        {
            transPen = new Pen(Brushes.Transparent, 1);
        }

        private static Pen transPen;

        protected override void OnRender(DrawingContext drawingContext)
        {
            if (MasterBt == null) return;

            Rect rect = new Rect(this.AdornedElement.RenderSize);

            Brush renderBrush = masterBt.ExForeground;
            Pen renderPen = new Pen(renderBrush, 1) { EndLineCap = PenLineCap.Square };

            Point ptLT = new Point(rect.Left + 2, rect.Top + 4);
            Point ptRT = new Point(ptLT.X + masterBt.Editor.ExFontSize / 2, ptLT.Y);
            Point ptLB = new Point(ptLT.X, rect.Bottom - 3);
            Point ptRB = new Point(ptRT.X, ptLB.Y);
            Point ptLT2 = new Point(ptLT.X + 2, ptLT.Y);
            Point ptLB2 = new Point(ptLB.X + 2, ptLB.Y);


            drawingContext.DrawRectangle(Brushes.Transparent, transPen, new Rect(ptLT, ptRB));

            if (IsMouseOver || masterBt.IsCollapsed)
            {
                drawingContext.DrawLine(renderPen, ptRT, ptLT);
                drawingContext.DrawLine(renderPen, ptLT, ptLB);
                drawingContext.DrawLine(renderPen, ptLT2, ptLB2);
                drawingContext.DrawLine(renderPen, ptLB, ptRB);
                //drawingContext.DrawRectangle(renderBrush, renderPen, new Rect(ptLT, ptLB2));
            }
        }
    }
}